package com.hh.usersystem.service.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hh.baseframework.util.Check;
import com.hh.baseframework.util.Convert;
import com.hh.baseframework.util.dto.PageRange;
import com.hh.hibernate.dao.inf.IHibernateDAO;
import com.hh.hibernate.util.dto.HQLParamList;
import com.hh.hibernate.util.dto.PagingData;
import com.hh.usersystem.bean.usersystem.HhXtCd;
import com.hh.usersystem.bean.usersystem.HhXtCz;
import com.hh.usersystem.bean.usersystem.HhXtJs;
import com.hh.usersystem.bean.usersystem.HhXtJsCd;
import com.hh.usersystem.bean.usersystem.HhXtJsCz;
import com.hh.usersystem.bean.usersystem.HhXtOrgJs;
import com.hh.usersystem.bean.usersystem.HhXtYhJs;

@Service
public class RoleService {
	@Autowired
	private IHibernateDAO<HhXtJs> xtjsdao;
	@Autowired
	private IHibernateDAO<HhXtYhJs> hhXtYhJsdao;
	@Autowired
	private IHibernateDAO<HhXtJsCd> hhXtJsCddao;
	@Autowired
	private IHibernateDAO<HhXtCd> xtcddao;
	@Autowired
	private IHibernateDAO<HhXtJsCz> hhxtjsczDao;
	@Autowired
	private IHibernateDAO<HhXtCz> xtczdao;
	@Autowired
	private IHibernateDAO<HhXtOrgJs> hhxtorgjsdao;

	public List<HhXtJs> queryAllRoleList() {
		return xtjsdao.queryList(HhXtJs.class,
				new HQLParamList().addCondition(Restrictions.eq("nzt", 0l)));
	}

	public PagingData<HhXtJs> queryPagingData(HhXtJs hhXtJs, String roles,
			PageRange pageRange) {
		HQLParamList hqlParamList = new HQLParamList();
		if (!Check.isEmpty(hhXtJs.getText())) {
			hqlParamList.add(Restrictions.like("text", "%" + hhXtJs.getText()
					+ "%"));
		}
		if (!Check.isEmpty(roles)) {
			hqlParamList.add(Restrictions.in("id", Convert.strToList(roles)));
		}
		// if (!Check.isEmpty(hhXtYh.getNxb())) {
		// hqlSearchCondition.getHqlParamList().add(
		// Restrictions.eq("nxb", hhXtYh.getNxb()));
		// }
		return xtjsdao.queryPagingData(HhXtJs.class, hqlParamList, pageRange);
	}

	public HhXtJs save(HhXtJs hhXtJs) {
		if (Check.isEmpty(hhXtJs.getId())) {
			xtjsdao.createEntity(hhXtJs);
		} else {
			xtjsdao.updateEntity(hhXtJs);
		}
		return hhXtJs;
	}

	public HhXtJs findObjectById(String id) {
		HhXtJs hhXtJs = xtjsdao.findEntityByPK(HhXtJs.class, id);
		return hhXtJs;
	}

	public void deleteByIds(String ids) {
		List<String> idList = Convert.strToList(ids);
		if (!Check.isEmpty(idList)) {
			xtjsdao.deleteEntity(HhXtJs.class, "id", idList);
			hhXtYhJsdao.deleteEntity(HhXtYhJs.class, "jsId", idList);
			hhXtJsCddao.deleteEntity(HhXtJsCd.class, "jsId", idList);
			hhxtjsczDao.deleteEntity(HhXtJsCz.class, "jsId", idList);
			hhxtorgjsdao.deleteEntity(HhXtOrgJs.class, "jsId", idList);
		}

	}

	public void insertOrdeleteMenu(Map<String, String> paramsMap) {

		if ("true".equals(paramsMap.get("checked"))) {
			HhXtJsCd hhXtJsCd = new HhXtJsCd();
//			HhXtCd hhXtCd = xtcddao.loadEntityByPK(HhXtCd.class,
//					paramsMap.get("menuid"));
			// hhXtCd.setId(paramsMap
			// .get("menuid"));
			hhXtJsCd.setCdId(paramsMap.get("menuid"));
			hhXtJsCd.setJsId(paramsMap.get("roleid"));
			hhXtJsCddao.createEntity(hhXtJsCd);
		} else {
			hhXtJsCddao
					.deleteEntity(
							"delete HhXtJsCd o where jsId=:roleid and cdId=:menuid",
							paramsMap);
		}
	}

	public void insertOrdeleteOperate(Map<String, String> paramsMap) {
		if ("true".equals(paramsMap.get("checked"))) {
			HhXtJsCz hhXtJsCd = new HhXtJsCz();
			HhXtCz hhXtCd = xtczdao.loadEntityByPK(HhXtCz.class,
					paramsMap.get("menuid"));
			// hhXtCd.setId(paramsMap
			// .get("menuid"));
			hhXtJsCd.setHhXtCz(hhXtCd);
			hhXtJsCd.setJsId(paramsMap.get("roleid"));
			hhxtjsczDao.createEntity(hhXtJsCd);
		} else {
			hhxtjsczDao
					.deleteEntity(
							"delete HhXtJsCz o where jsId=:roleid and hhXtCz.id=:menuid",
							paramsMap);
		}
	}

	public void updateOperateLevel(Map<String, String> paramsMap) {
		if ("true".equals(paramsMap.get("checked"))) {
			hhxtjsczDao
					.updateEntity(
							"update HhXtJsCz o set o.operLevel=:operateLevel where o.jsId=:roleid and o.hhXtCz.id=:menuid",
							paramsMap);
		} else {
			hhxtjsczDao
					.updateEntity(
							"update HhXtJsCz o set o.operLevel='' where o.jsId=:roleid and o.hhXtCz.id=:menuid",
							paramsMap);
		}
	}
}
